package com.qsl.ssyx;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池-创建异步对象
 * 有两种格式，一种是supply开头的方法，一种是run开头的方法
 * - supply开头：这种方法，可以返回异步线程执行之后的结果。
 * - run开头：这种不会返回结果，就只是执行线程任务
 * ======》 本次为supply
 */
public class CompletableFutureDemo1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建线程-线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        System.out.println(" main begin...");
        CompletableFuture<Object> future = CompletableFuture.supplyAsync(
                () -> {
                    System.out.println("当前线程名称：" + Thread.currentThread().getName());
                    int result = 1024;
                    System.out.println("result: " + result);
                    return result;
                }, executorService);
//        获取返回结果
        Object value = future.get();
        System.out.println(" main over ...." + value);
    }
}
